<?php

namespace Hyperf\AsyncQueue;

use App\Job\Job;
use Serializable;

class Message implements Serializable
{
    /**
     * @var Job
     */
    protected $job;

    /**
     * @var int
     */
    protected $attempts = 0;

    public function __construct(Job $job)
    {
        $this->job = $job;
    }

    public function job(): Job
    {
        return $this->job;
    }

    public function attempts(): bool
    {
        if ($this->job->getMaxAttempts() > $this->attempts++) {
            return true;
        }
        return false;
    }

    public function getAttempts(): int
    {
        return $this->attempts;
    }

    public function serialize()
    {
        $this->job = $this->job->compress();
        return serialize([$this->job, $this->attempts]);
    }

    public function unserialize($serialized)
    {
        [$job, $attempts] = unserialize($serialized);
        $job = $job->uncompress();

        $this->job = $job;
        $this->attempts = $attempts;
    }
}
